package lancs.mobilemedia.core.ui.controller;

import java.util.Vector;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;

import lancs.mobilemedia.core.ui.MainUIMidlet;
import lancs.mobilemedia.core.ui.datamodel.AlbumData;
import lancs.mobilemedia.core.ui.datamodel.ImageData;
import lancs.mobilemedia.core.ui.screens.AlbumListScreen;
import lancs.mobilemedia.core.ui.screens.PhotoListScreen;
import lancs.mobilemedia.core.util.Constants;
import lancs.mobilemedia.factories.FavouritesSPLFactory;
import lancs.mobilemedia.factories.SPLFactory;
import lancs.mobilemedia.factories.SortingSPLFactory;
import lancs.mobilemedia.lib.exceptions.UnavailablePhotoAlbumException;

public class PhotoListController extends AbstractController {

	private ControllerCommand command;
	private ControllerMapper photoListControllerMapper;

	private Vector photoListControllerFactories;
	private ImageLister imageLister;

	public PhotoListController(MainUIMidlet midlet, AlbumData albumData, AlbumListScreen albumListScreen) {
		super(midlet, albumData, albumListScreen);
		init();
	}

	private void init() {
		createFactories();
		createControllerMapper();
		createImageLister();
	}

	private void createFactories() {
		photoListControllerFactories = new Vector();
		// if Sorting feature is enabled
		photoListControllerFactories.addElement(new SortingSPLFactory());
		// if Favourites feature is enable
		photoListControllerFactories.addElement(new FavouritesSPLFactory());
	}

	private void createControllerMapper() {
		photoListControllerMapper = new PhotoListControllerMapper();
	}

	private void createImageLister() {
		imageLister = new DefaultImageLister();
		SPLFactory factory;
		for (int i = 0; i < photoListControllerFactories.size(); i++) {
			factory = (SPLFactory) photoListControllerFactories.elementAt(i);
			imageLister = factory.createImageLister(imageLister);
		}
	}

	public boolean handleCommand(Command c) {
		String label = c.getLabel();
		System.out.println(this.getClass().getName() + "::handleCommand: " + label);

		command = (ControllerCommand) photoListControllerMapper.getCommand(label);

		if (command != null) {
			return command.execute(this);
		}
		return false;
	}

	public void showImageList(String recordName, boolean sort, boolean favorite) {
		if (recordName == null)
			recordName = getCurrentStoreName();

		PhotoController photoController = new PhotoController(midlet, getAlbumData(), getAlbumListScreen());
		photoController.init();
		photoController.setNextController(this);

		PhotoListScreen imageList = new PhotoListScreen();
		imageList.setCommandListener(photoController);

		imageList.initMenu();

		ImageData[] images = null;
		try {
			images = getAlbumData().getImages(recordName);
		} catch (UnavailablePhotoAlbumException e) {
			Alert alert = new Alert("Error", "The list of photos can not be recovered", null, AlertType.ERROR);
			Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
			return;
		}

		if (images == null)
			return;

		this.init();
		imageLister.sort(imageList, images, sort);

		for (int i = 0; i < images.length; i++) {
			if (images[i] != null) {
				imageLister.createImageList(imageList, images[i], favorite);
			}
		}

		setCurrentScreen(imageList);
	}

	public boolean goToPreviousScreen() {
		String currentScreenName = ScreenSingleton.getInstance().getCurrentScreenName();
		if ((currentScreenName.equals(Constants.IMAGELIST_SCREEN))
				|| (currentScreenName.equals(Constants.NEWALBUM_SCREEN))) {
			getAlbumListScreen().repaintListAlbum(getAlbumData().getAlbumNames());
			setCurrentScreen(getAlbumListScreen());
			ScreenSingleton.getInstance().setCurrentScreenName(Constants.ALBUMLIST_SCREEN);
			return true;
		}

		return false;
	}
}